package redis.session ;

import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpSession ;

/**
 * 重写 HttpServletRequestWrapper
 * @author 80002165 @date 2017年7月5日 上午10:05:41
 */
public class HttpServletRequestWrapper extends org.apache.catalina.servlet4preview.http.HttpServletRequestWrapper {
    
    HttpSession session ;
    private HttpServletRequest request ;
    private HttpServletResponse response ;
    private String sid = "" ;
    
    /**
     * @param request
     */
    public HttpServletRequestWrapper(HttpServletRequest request) {
        super(request) ;
    }
    
    public HttpServletRequestWrapper(String sid, HttpServletRequest request, HttpServletResponse response) {
        super(request) ;
        this.sid = sid ;
        this.request = request ;
        this.response = response ;
        if (this.session == null) {
            this.session = new HttpSessionWrapper(this.sid, super.getSession(false), this.request, this.response) ;
        }
    }
    
    @Override
    public HttpSession getSession() {
        if (this.session == null) {
            this.session = new HttpSessionWrapper(this.sid, super.getSession(), this.request, this.response) ;
        }
        return this.session ;
    }
    
    @Override
    public HttpSession getSession(boolean create) {
        if (this.session == null) {
            if (create) {
                this.session = new HttpSessionWrapper(this.sid, super.getSession(create), this.request, this.response) ;
            } else {
                return null ;
            }
        }
        return this.session ;
    }
    
}
